package com.fly_fox.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fly_fox.dao.TaskItemDao;
import com.fly_fox.dto.TaskGroupDto;
import com.fly_fox.entity.TaskItem;
import com.fly_fox.util.JdbcUtil;
import com.fly_fox.util.SafeProperties;
import com.fly_fox.util.TimeUtil;
import com.sun.jndi.toolkit.url.Uri;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.dao.DataAccessException;
import org.yaml.snakeyaml.Yaml;

import java.io.InputStream;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

public class TaskItemService {

    private List<TaskGroupDto> taskGroupDtoList= Collections.EMPTY_LIST;



    public void loadData(){

    }

    /**
     * 加载yaml的数据
     */
    public void loadDefaultDataYaml(){
        try {
            URL url = ClassLoader.getSystemResource("code_learn.yml");
            Path ymlPath = Paths.get(url.toURI());
            byte[] bytes = Files.readAllBytes(ymlPath);
            String str=new String(bytes, StandardCharsets.UTF_8);
            /*System.out.println(str);*/

            //  yaml转json
            Yaml yaml=new Yaml();
            Object load = yaml.load(str);
            String jsonStr = JSONObject.toJSONString(load);
            taskGroupDtoList= JSON.parseArray(jsonStr,TaskGroupDto.class);
            /*for (TaskGroupDto taskGroupDto : taskGroupDtoList) {
                System.out.println(taskGroupDto);
            }*/

        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public void loadDataYaml(Path ymlPath){
        try {
            byte[] bytes = Files.readAllBytes(ymlPath);
            String str=new String(bytes, StandardCharsets.UTF_8);
            /*System.out.println(str);*/

            //  yaml转json
            Yaml yaml=new Yaml();
            Object load = yaml.load(str);
            String jsonStr = JSONObject.toJSONString(load);
            taskGroupDtoList= JSON.parseArray(jsonStr,TaskGroupDto.class);
            /*for (TaskGroupDto taskGroupDto : taskGroupDtoList) {
                System.out.println(taskGroupDto);
            }*/

        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public TaskGroupDto getGroupName(String dateStr){
        TimeUtil.TimeUse<TaskGroupDto> timeUtil=new TimeUtil.TimeUse<TaskGroupDto>();
        timeUtil.setInterval(1);
        timeUtil.setItems(taskGroupDtoList);
        timeUtil.setStartDate(LocalDate.parse("2022-06-01"));
        TaskGroupDto item = timeUtil.ret(LocalDate.parse(dateStr));
        return item;
    }

    public TaskItem random(TaskGroupDto groupDto){
        if(groupDto.getItems().isEmpty()){
            throw new RuntimeException("改组任务没有子项:"+groupDto.getTitle());
        }
        List<TaskItem> list= TaskItemDao.listByGroup(groupDto.getTitle());
        //  为空的话就先往数据库插入一次数据
        if(list.isEmpty()){
            insertGroup(groupDto);
            list= TaskItemDao.listByGroup(groupDto.getTitle());
        }

        int index=RandomUtils.nextInt(0,list.size());
        TaskItem item=list.get(index);
        //   删除数据库的数据
        TaskItemDao.delByid(item.getId());
        return item;


    }

    public void insertOne(String groupName,String item){
        TaskItem taskItem=new TaskItem();
        taskItem.setGroupName(groupName);
        taskItem.setItemName(item);
        taskItem.setType("code_learn");
        taskItem.setExecStatus(1);
        JdbcUtil.ins.insert(taskItem);
    }

    public void insertGroup(TaskGroupDto dto){
        for (String item : dto.getItems()) {
            insertOne(dto.getTitle(),item);
        }
    }



    public void print(){
        System.out.println(taskGroupDtoList);
    }

}
